import java.sql.PseudoColumnUsage;
import java.util.Arrays;

public class MyArrayList implements IList {
    public int[] elem;
    public int usedSize;

    @Override
    public boolean isFull() {
        return usedSize == elem.length;
    }

    @Override
    public void add(int data) {
        if(isFull()){
            //如果满了，就扩容为原来的两倍
            elem = Arrays.copyOf(elem,elem.length*2);
        }
        elem[usedSize] = data;
        usedSize++;
    }

    @Override
    public void add(int pos, int data) {
        //判断输入的 pos 是否合法
        try{
            checkAddPos(pos);
        }catch (PosNotLegalException e){
            e.printStackTrace();
        }
        //判断数组是否装满
        if(isFull()){
            elem = Arrays.copyOf(elem,elem.length*2);
        }
        //移动元素
        for (int i = usedSize - 1; i >= pos; i--) {
            elem[i + 1] = elem[i];
        }
        //插入元素
        elem[pos] = data;
        usedSize++;
    }

    private void checkAddPos(int pos) throws PosNotLegalException{
        if(pos < 0 || pos > usedSize){
            throw new PosNotLegalException("pos位置不合法");
        }
    }

    @Override
    public boolean contain(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int data) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == data)
                return i;
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        //判断 pos 是否合法
        try{
            checkGetAndSetPos(pos);
        }catch (PosNotLegalException e){
            e.printStackTrace();
        }
        return elem[pos];
    }

    private void checkGetAndSetPos (int pos) throws PosNotLegalException{
        if(pos < 0 || pos >= usedSize){
            throw new PosNotLegalException("pos位置不合法");
        }
    }

    @Override
    public void set(int pos, int value) {
        try{
            checkGetAndSetPos(pos);
        }catch (PosNotLegalException e){
            e.printStackTrace();
        }
        elem[pos] = value;
    }

    @Override
    public void remove(int key) {
        int pos = indexOf(key);
        if(pos == -1){
            System.out.println("没有要找的关键字");
            return;
        }
        for (int i = pos; i < usedSize - 1; i++) {
            elem[i] = elem[i + 1];
        }
        usedSize--;
    }

    @Override
    public void removeAll(int key) {
        for (int i = 0; i < usedSize; i++) {
            int pos = indexOf(key);
            if(pos == -1){
                System.out.println("没有要找的关键字");
                return;
            }
            for (int j = pos; j < usedSize - 1; j++) {
                elem[j] = elem[j + 1];
            }
            usedSize--;
        }
    }

    @Override
    public void clear() {
        usedSize = 0;
    }
}
